package com.konsung.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.konsung.bean.PatientBean;
import com.konsung.healthfile.R;
import com.konsung.ui.PatientDownloadActivity;
import com.konsung.ui.defineview.RoundImageView;
import com.konsung.util.IdCardUtil;
import com.konsung.util.UiUtils;
import com.konsung.util.constant.PatientStyle;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 类功能：居民下载适配器
 * @author xiangshicheng
 */

public class PatienDownloadListAdapter extends BaseAdapter {

    private final Context context;
    private List<PatientBean> patients = null; //每页显示的数据
    private LayoutInflater layoutInflater = null;
    private boolean isCheckAll = false; //是否全选 ,默认不全选
    private boolean isOpreateAllSelectBtn = false; //标识是否点击了全选按钮，不管是选中还是未选中，只要点击了则为true

    /**
     * 构造器
     * @param context 上下文
     * @param patients 文本数据集
     */
    public PatienDownloadListAdapter(Context context, List<PatientBean> patients) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.patients = patients;
    }

    @Override
    public int getCount() {
        if (patients != null) {
            return patients.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return patients.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_download_list, null);
            ButterKnife.bind(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final PatientBean patientBean = patients.get(position);
        viewHolder.tvSerial.setText(position + 1 + "");
        viewHolder.tvAge.setText(patientBean.getAge() + UiUtils.getString(R.string.year));
        String name = patientBean.getName();
        viewHolder.tvName.setText(name);
        final String idcard = patientBean.getIdcard();
        viewHolder.tvIdCardNumber.setText(idcard);
        viewHolder.tvSex.setText(IdCardUtil.getSexFormDB(patientBean.getSex()));
        switch (patientBean.getPatientType()) {
            case PatientStyle.ADULT:
                viewHolder.tvPatientType.setText(R.string.type_adult);
                break;
            case PatientStyle.YOUTH:
                viewHolder.tvPatientType.setText(R.string.type_youth);
                break;
            case PatientStyle.BABY:
                viewHolder.tvPatientType.setText(R.string.type_baby);
                break;
            default:
                break;
        }
        UiUtils.setHeadPhoto(viewHolder.ivIcon, patientBean);
        if (isCheckAll) {
            viewHolder.cbChoose.setChecked(isCheckAll);
            //如果有取消掉的，回显不显示
            List<String> cancelIdcardList = getIdCardByPatient(((PatientDownloadActivity) context)
                    .getHandCancelList());
            if (cancelIdcardList.contains(idcard)) {
                viewHolder.cbChoose.setChecked(false);
            } else {
                viewHolder.cbChoose.setChecked(true);
            }
        } else {
            List<String> idcardList = getIdCardByPatient(((PatientDownloadActivity) context)
                    .getSelectPatientList());
            if (idcardList.contains(idcard)) {
                viewHolder.cbChoose.setChecked(true);
            } else {
                viewHolder.cbChoose.setChecked(false);
            }
        }
        viewHolder.cbChoose.setTag(position);
        viewHolder.cbChoose.setOnCheckedChangeListener(new CompoundButton
                .OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                Log.e("xsc_isOp", "" + isOpreateAllSelectBtn);
                Log.e("xsc_position", position + "-" + patients.size());
                if (isOpreateAllSelectBtn) {
                    //操作了全选按钮的情况下不走该回调
                } else {
                    if (isCheckAll) {
                        //全选选中时的数据处理
                        ((PatientDownloadActivity) context).setHandCancelList(patientBean
                                , isChecked);
                    } else {
                        //未选中全选的数据处理
                        ((PatientDownloadActivity) context).setSelectPatient(patientBean
                                , isChecked);
                    }
                }
            }
        });
        int mPosition = (int) viewHolder.cbChoose.getTag();
        Log.e("xsc_mposition", "" + mPosition);
        if (mPosition == (patients.size() - 1)) {
            isOpreateAllSelectBtn = false;
        }
        return convertView;
    }

    /**
     * 视图容器
     */
    class ViewHolder {
        @BindView(R.id.tv_serial)
        TextView tvSerial; //序号
        @BindView(R.id.tv_name)
        TextView tvName; //姓名
        @BindView(R.id.tv_id_card_number)
        TextView tvIdCardNumber; //身份证号
        @BindView(R.id.tv_sex)
        TextView tvSex; //性别
        @BindView(R.id.tv_age)
        TextView tvAge; //年龄
        @BindView(R.id.tv_patient_type)
        TextView tvPatientType; //居民类型
        @BindView(R.id.iv_icon)
        RoundImageView ivIcon; //头像
        @BindView(R.id.cbx_select)
        CheckBox cbChoose; //勾选框
    }

    /**
     * 设置数据
     * @param list 数据
     */
    public void setDatas(List<PatientBean> list) {
        patients = list;
        notifyDataSetChanged();
    }

    /**
     * 设置是否全选
     * @param checkAll 是否全选标记
     */
    public void setCheckAll(boolean checkAll) {
        this.isCheckAll = checkAll;
        isOpreateAllSelectBtn = true;
        notifyDataSetChanged();
    }

    /**
     * 设置全选按钮操作状态值
     * @param state 值
     */
    public void setValue(boolean state) {
        isOpreateAllSelectBtn = state;
    }

    /**
     * 通过用户获取身份证
     * @param list 用户数据集合
     * @return 身份证集合
     */
    private List<String> getIdCardByPatient(List<PatientBean> list) {
        List<String> stringList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (PatientBean patientBean : list) {
                stringList.add(patientBean.getIdcard());
            }
        }
        return stringList;
    }
}
